from time import sleep
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.firefox.service import Service


def get_alert_msg():
    '''获取弹窗信息方法'''
    msg = DriverUtil.get_driver().find_element(By.CLASS_NAME, 'layui-layer-content').text
    return msg

class DriverUtil(object):
    '''浏览器对象管理类'''
    __driver = None   # 浏览器对象变量初始化状态

    @classmethod
    def get_driver(cls):
        '''获取浏览器对象方法'''
        if cls.__driver is None:
            cls.__driver_path = "C:/Users/Lenovo/AppData/Local/Programs/Python/Python310/geckodriver.exe"
            service = Service(executable_path=cls.__driver_path)
            cls.__driver = webdriver.Firefox(service=service)
            cls.__driver.maximize_window()    # 最大化窗口
            cls.__driver.implicitly_wait(10)  # 隐式等待
        return cls.__driver

    @classmethod
    def quit_driver(cls):
        '''退出浏览器对象方法'''
        if cls.__driver:
            cls.__driver.quit()
            cls.__driver = None

if __name__ == '__main__':
    DriverUtil.get_driver()     # 获取浏览器对象
    sleep(2)
    DriverUtil.quit_driver()   # 退出浏览器对象